<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\member\home;
use app\index\controller\User;
use app\index\model\GetCityProper as GetCityProperModel;

use think\Db;
/**             
 * 行为管理控制器
 * @package app\admin\controller
 */
class Memberinfo extends User
{
    //会员信息
    public function index()
    {
        $id=session('uid');
        //个人信息
        $info = Db::name('member')->where("id=$id")->find();
        $this->assign('info',$info);

        //发票信息
        $mb = Db::name('member_balance')->where("memberid=$id")->find();
        $this->assign('mb',$mb);

        //营业执照信息
        $bl = Db::name('business_license')->where("memberid=$id")->find();
        $this->assign('bl',$bl);

        //公司信息
        $cn = Db::name('company_nature')->where("id=$info[company_nature]")->find();
        $this->assign('cn',$cn);

        return $this->fetch('my-info');
    }

        public function infoChange(){
                $memberinfo=session('uid');
                $member_id=$memberinfo;
                $info=Db::name('member')->where('id',$member_id)->find();
                $this->assign('info',$info);
                return $this->fetch('info_change');

    }
    //修改用户信息
        public function editInfo()
        {
                $memberinfo = session('uid');
                $member_id = $memberinfo;
                $data['user_name'] = input('post.username');
                $data['mobile'] = input('post.moblie');
                $data['email'] = input('post.email');
                $data['qq'] = input('post.qq');
                $data['name'] = input('post.name');
                $data['l_mobile'] = input('post.lmobile');
                $phone = '/^1[0-9]{10}$/';
                $email = '/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/';
                $res_phone = preg_match($phone, $data['mobile']);
                $res_lphone = preg_match($phone, $data['l_mobile']);
                $res_email = preg_match($email, $data['email']);
                if ($res_phone == 0) {
                    echo 1;
                    exit();
                }
                if ($res_email == 0) {
                    echo 3;
                    exit();
                }
                if ($res_lphone == 0) {
                    echo 2;
                    exit();
                }
                $res = Db::name('member')->where('id',$member_id)->update($data);
                if($res){
                    echo 4;exit();
                }else{
                    echo 5;exit();
                }
        }
        public function companyChange(){
                $id=session('uid');
                $info = Db::name('member')->where("id=$id")->find();

                $list_province = GetCityProperModel::getProvince();
                $where['pid']=$info['provinceid'];
                $where['status']=1;
                $list_city=GetCityProperModel::getCityProper($where);
                $where1['pid']=$info['cityid'];
                $where1['status']=1;
                $list_area=GetCityProperModel::getCityProper($where1);
                $industry = Db::name('member_industry')->where('status=1')->select();//行业
                $company_nature = Db::name('company_nature')->where('status=1')->select();//性质
                $this->assign('province', $list_province);
                $this->assign('city',$list_city);
                $this->assign('area',$list_area);
                $this->assign('company_info',$info);
                $this->assign('industry', $industry);
                $this->assign('company_nature', $company_nature);
                return $this->fetch('company_change');
        }
    //更改公司信息
        public function companyEdit(){
                $memberinfo = session('uid');
                $member_id = $memberinfo;
                $data['company'] = input('post.company');
                $data['provinceid'] = input('post.province');
                $data['cityid'] = input('post.city');
                $data['townid'] = input('post.town');
                $data['addr'] = input('post.addr');
                $data['company_nature'] = input('post.company_nature');
                $data['industry'] = input('post.industry');
                $where['id']=$data['provinceid'];
                $where['status']=1;
                $provice=GetCityProperModel::getPlace($where);
                $data['province']=$provice['name'];
                $map['id']=$data['cityid'];
                $map['status']=1;
                $city=GetCityProperModel::getPlace($map);
                $data['city']=$city['name'];
                $list['id']=$data['townid'];
                $list['status']=1;
                $town=GetCityProperModel::getPlace($list);
                $data['town']=$town['name'];
                if(empty($data['addr'])){
                    if(empty($town['name'])){
                        $data['addr']=$provice['name'].$city['name'];
                    }else{
                        $data['addr']=$provice['name'].$city['name'].$town['name'];
                    }
                }else{
                    if(strpos($data['addr'],'省') || strpos($data['addr'],'市')){
                        $data['addr']=$data['addr'];
                    }else{
                        if(empty($town['name'])){
                            $data['addr']=$provice['name'].$city['name'].$data['addr'];
                        }else{
                            $data['addr']=$provice['name'].$city['name'].$town['name'].$data['addr'];
                        }
                    }
                }
                $res = Db::name('member')->where('id',$member_id)->update($data);
                if($res){
                    echo 1;exit();
                }else{
                    echo 2;exit();
                }
        }
        public function invoiceChange()
        {
            $id = session('uid');
            $balance = Db::name('member_balance')->where("memberid=$id")->find();
                $list_province = GetCityProperModel::getProvince();
                $where['pid']=$balance['c_provinceid'];
                $where['status']=1;
                $list_city=GetCityProperModel::getCityProper($where);
                $where1['pid']=$balance['c_cityid'];
                $where1['status']=1;
                $list_area=GetCityProperModel::getCityProper($where1);
                $map['pid']=$balance['provinceid'];
                $map['status']=1;
                $c_city=GetCityProperModel::getCityProper($map);
                $map1['pid']=$balance['cityid'];
                $map1['status']=1;
                $c_area=GetCityProperModel::getCityProper($map1);
                $invoiceType=Db::name('invoice_type')->where('status=1')->select();
                $this->assign('c_city',$list_city);
                $this->assign('c_area',$list_area);
                $this->assign('city',$c_city);
                $this->assign('area',$c_area);
                $this->assign('list_province',$list_province);
                $this->assign('balance',$balance);
                $this->assign('invoicetype',$invoiceType);
                return $this->fetch('invoice_change');
        }
    //更改开票信息invoiceEdit
    public function invoiceEdit(){
        $id = session('uid');
        $balance = Db::name('member_balance')->where("memberid=$id")->find();
        $data['invoice_type']=input('post.type');
        $data['company']=input('post.company');
        if($data['invoice_type']==1){
            $data['accountname']=input('post.name');
            $data['mobile']=input('post.tel');
            $data['bankname']=input('post.bankname');
            $data['c_provinceid']=input('post.c_province');
            $data['c_cityid']=input('post.c_city');
            $data['c_townid']=input('post.c_area');
            $data['provinceid']=input('post.province');
            $data['cityid']=input('post.city');
            $data['townid']=input('post.area');
            $data['taxpayer_no']=input('post.taxpayer_no');
            $data['bankid']=input('post.bankid');
            $bandaddr=input('post.bandaddr');
            $signup_address=input('post.signup_address');
            $where1['id']=$data['c_provinceid'];
            $where1['status']=1;
            $c_provinceid=GetCityProperModel::getPlace($where1);
            $where2['id']=$data['c_cityid'];
            $where2['status']=1;
            $c_cityid=GetCityProperModel::getPlace($where2);
            $where3['id']=$data['c_townid'];
            $where3['status']=1;
            $c_townid=GetCityProperModel::getPlace($where3);
            $where4['id']=$data['provinceid'];
            $where4['status']=1;
            $provinceid=GetCityProperModel::getPlace($where4);
            $where5['id']=$data['cityid'];
            $where5['status']=1;
            $cityid=GetCityProperModel::getPlace($where5);
            $where6['id']=$data['townid'];
            $where6['status']=1;
            $townid=GetCityProperModel::getPlace($where6);
            $phone = '/^1[0-9]{10}$/';
            $res_phone = preg_match($phone, $data['mobile']);
            if($res_phone==0){
                echo 3;exit();
            }
            if(empty($bandaddr)){
                if(empty($data['c_townid'])){
                    $data['bandaddr']=$c_provinceid['name'].$c_cityid['name'];
                }else{
                    $data['bandaddr']=$c_provinceid['name'].$c_cityid['name'].$c_townid['name'];
                }
            }else{
                if(strpos($bandaddr,'省') || strpos($bandaddr,'市')){
                    $data['bandaddr']=$bandaddr;
                }else{
                    if(empty($data['c_townid'])){
                        $data['bandaddr']=$c_provinceid['name'].$c_cityid['name'].$bandaddr;
                    }else{
                        $data['bandaddr']=$c_provinceid['name'].$c_cityid['name'].$c_townid['name'].$bandaddr;
                    }
                }
            }
            if(empty($signup_address)){
                if(empty($data['townid'])){
                    $data['signup_address']=$provinceid['name'].$cityid['name'];
                }else{
                    $data['signup_address']=$provinceid['name'].$cityid['name'].$townid['name'];
                }
            }else{
                if(strpos($signup_address,'省') || strpos($signup_address,'市')){
                    $data['signup_address']=$signup_address;
                }else{
                    if(empty($data['townid'])){
                        $data['signup_address']=$provinceid['name'].$cityid['name'].$signup_address;
                    }else{
                        $data['signup_address']=$provinceid['name'].$cityid['name'].$townid['name'].$signup_address;
                    }
                }
            }

        }
        $map['balanceid']=$balance['balanceid'];
        $res = Db::name('member_balance')->where($map)->update($data);
        if($res){
            echo 1;exit();
        }else{
            echo 2;exit();
        }
    }

}